/*
* SMILParser
*
* Version 1.0
*
* Programmers: Jesse Louderback, Noah Pham, Chad Carpenter, and Marc Hindman
*/
package com.SmilDial;

import parser.SMILParser;
import Message.SMILMessage;
import Message.SMILObject;
import Message.SMILRegion;
import MessageView.MessagePlayer;
import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.SubMenu;

public class ViewMessage extends Activity {
	final String FILE_LOC = Environment.getExternalStorageDirectory().getAbsolutePath()
							+ "/SMIL Messages/";
	private String file = "";
	private MessagePlayer dv;

	// edit variables
	Bundle SMIL_Bundle;
	SMILMessage message;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		SMIL_Bundle = getIntent().getExtras();
		if (SMIL_Bundle.size() == 1) {
			file = SMIL_Bundle.getString("file");

			SMILParser parse = new SMILParser(FILE_LOC + file);
			message = parse.parse();
			dv = new MessagePlayer(this, message);
			dv.setFin(BitmapFactory.decodeResource(getResources(), R.drawable.fin));
			setContentView(dv);
		} else {
			
			// the message
			message = new SMILMessage();
			message.setEdit(true);
			
			// get text
			SMILObject text = new SMILObject("text", SMIL_Bundle.getFloat("txtStartTime"),
											 SMIL_Bundle.getFloat("txtDurationTime"),
											 SMIL_Bundle.getString("txtInput"));

			// get image
			SMILObject img = new SMILObject("img", SMIL_Bundle.getFloat("imgStartTime"),
											SMIL_Bundle.getFloat("imgDurationTime"),
											SMIL_Bundle.getString("imgImageURL"));

			// get sound
			SMILObject audio = new SMILObject("audio", SMIL_Bundle.getFloat("soundStartTime"),
											  SMIL_Bundle.getFloat("soundDurationTime"),
											  SMIL_Bundle.getString("soundURL"));

			// set up regions
			SMILRegion textRegion = null, imgRegion;
			if (!text.getData().equals("")) {
				textRegion = new SMILRegion("textReg", 50, 50, 0, 0);
				text.setRegion(textRegion.getId());
			}
			
			imgRegion = new SMILRegion("imgReg", 0, 0, 50, 50);
			img.setRegion(imgRegion.getId());

			message.AddObject(text);
			message.AddObject(img);
			if (!audio.getData().equals("")) {
				message.AddObject(audio);
			}
			message.addRegion(textRegion.getId(), textRegion);
			message.addRegion(imgRegion.getId(), imgRegion);

			dv = new MessagePlayer(this, message, true);
			setContentView(dv);
		}

	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		if (!message.getEdit()) {
			switch (item.getItemId()) {
			case R.id.pause:
				if (item.getTitle().equals("Pause")) {
					dv.pauseMessage();
					item.setTitle("Resume");
				} else if (item.getTitle().equals("Resume")) {
					dv.resumeMessage();
					item.setTitle("Pause");
				}
				break;
				
			case R.id.stop:
				finish();
				break;
				
			case R.id.reset:
				dv.restartMessage();
				break;
				
			default:
				break;
			}
		} else {
			if (item.getTitle().equals("Image")) {
				message.setItemToEdit("img");
			} else if (item.getTitle().equals("Text")) {
				message.setItemToEdit("text");
			} else if (item.getTitle().equals("Finalize Message")) {
				FinalizeMenu.finalMessage = message.toString();
				finish();
			}
		}

		return true;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater(); // from activity
		if (!message.getEdit()) {
			inflater.inflate(R.menu.message_menu, menu);
		} else {
			super.onCreateOptionsMenu(menu);
			SubMenu sm;
			sm = menu.addSubMenu("Select Object");
			sm.add("Image");
			sm.add("Text");
			menu.add(1, 5, 1, "Finalize Message");
		}

		return true;

	}

	@Override
	protected void onDestroy() {
		dv.destroy();
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}
}
